<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/2 0002
 * Time: 下午 8:08
 */
$link = mysql_connect('localhost','root','123456');
if (!$link){
    die('连接数据库失败！'.mysql_error());
}
mysql_query('set names utf8');
mysql_query('use `itcast`');
$city = array('北京','上海','广州','其他');
$skill = array('HTML','JavaScript','PHP','C++');
$id = 1;
$sql = "select `nickname`, `gender` ,`email`, `qq`, `url`, `city`, 
         `skill`, `description` form `userinfo` where `id`=$id ";
$rst = mysql_query($sql);
$data = mysql_fetch_assoc($rst);
$data['skill'] = explode(',',$data['skill']);
require 'profile_html.php';

if (!empty($_POST)){
    $fields = array('nickname','gender','email','qq','url','city','skill','description');
    foreach ($fields as $v){
        $save_data[$v] = isset($_POST[$v]) ? $_POST[$v] :'';
    }
    if($save_data['gender']!='男'&& $save_data['gender']!='女'){
        die('保存失败:未选择性别。');
    }
    if(is_array($save_data['skill'] )){
        $save_data['skill'] = array_intersect($skill,$save_data['skill']);
        $save_data['skill'] = implode(',',$save_data['skill']);
    }else{
        $save_data['skill'] ='';
    }
    $sql = "update `userinfo` set ";
    foreach ($save_data as $k=>$v){
        $sql .="`$k`='".mysql_real_escape_string($v). " ',";
    }
    $sql = rtrim($sql,',')." where id=$id";
    $rst = mysql_query($sql);
    echo  $rst ?"保存成功: $sql" : "保存失败 : $sql<br>".mysql_errno();
    
}